package sk.stuba.fiit.foo07.genex.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

import sk.stuba.fiit.foo07.genex.beans.Answer;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class NewAnswerDialog extends javax.swing.JDialog {

    /**
     * 
     */
    private static final long serialVersionUID = 2716343263084921488L;

    private Answer ret;

    private JButton jBtnCancel;
    private JTextArea jTxtAnswer;
    private JSeparator jSeparator2;
    private JButton jBtnOK;
    private JScrollPane jScrAnswer;
    private JLabel jLabel1;

    public NewAnswerDialog(JDialog frame, Answer ans) {
        super(frame);

        if (ans != null)
            ret = ans;
        else
            ret = new Answer();

        initGUI();

        if (ret.getText() != null) {
            setTitle("Upraviť odpoveď");
            jTxtAnswer.setText(ret.getText());
            jTxtAnswer.setCaretPosition(0);
        } else
            setTitle("Nová odpoveď");
    }

    private void initGUI() {
        try {
            {
                getContentPane().setLayout(null);
                this.setResizable(false);
                this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                setLocationByPlatform(true);
                {
                    jLabel1 = new JLabel();
                    getContentPane().add(jLabel1);
                    jLabel1.setText("Text odpovede");
                    jLabel1.setBounds(12, 12, 175, 14);
                    jLabel1.setForeground(new java.awt.Color(100, 0, 0));
                }
                {
                    jScrAnswer = new JScrollPane();
                    getContentPane().add(jScrAnswer);
                    jScrAnswer.setBounds(12, 32, 371, 116);
                    {
                        jTxtAnswer = new JTextArea();
                        jScrAnswer.setViewportView(jTxtAnswer);
                        jTxtAnswer.setBackground(new java.awt.Color(255, 255,
                                240));
                    }
                }
                {
                    jBtnCancel = new JButton();
                    getContentPane().add(jBtnCancel);
                    jBtnCancel.setText("Cancel");
                    jBtnCancel.setBounds(287, 163, 96, 21);
                    jBtnCancel.setMnemonic(java.awt.event.KeyEvent.VK_C);
                    jBtnCancel.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            jBtnCancelActionPerformed(evt);
                        }
                    });
                }
                {
                    jBtnOK = new JButton();
                    getContentPane().add(jBtnOK);
                    jBtnOK.setText("OK");
                    jBtnOK.setBounds(186, 163, 96, 21);
                    jBtnOK.setMnemonic(java.awt.event.KeyEvent.VK_O);
                    jBtnOK.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            jBtnOKActionPerformed(evt);
                        }
                    });
                }
                {
                    jSeparator2 = new JSeparator();
                    getContentPane().add(jSeparator2);
                    jSeparator2.setBounds(12, 154, 368, 9);
                }
            }
            this.setSize(403, 231);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jBtnCancelActionPerformed(ActionEvent evt) {
        ret = null;
        setVisible(false);
    }

    private void jBtnOKActionPerformed(ActionEvent evt) {
        if (jTxtAnswer.getText().length() == 0) {
            JOptionPane.showMessageDialog(this, "Musíte zadať text odpovede.",
                    "Chyba", JOptionPane.ERROR_MESSAGE);
            return;
        }

        ret.setText(jTxtAnswer.getText());
        ret.setIsCorrect(false);

        setVisible(false);
    }

    public Answer showDialog() {
        this.setModalityType(ModalityType.DOCUMENT_MODAL);

        setVisible(true);

        return ret;
    }

}
